<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var validateStackSequences = function (pushed, popped) {
      const stack = []; // 栈
      let i = 0;
      for (e of pushed) {
        stack.push(e); // 入栈
        while (stack.length > 0 && stack.at(-1) === popped[i]) {
          // 栈不空，并且栈顶元素为出栈元素，执行出栈操作
          stack.pop();
          i++;
        }
      }
      // 栈空，说明符合条件
      return stack.length === 0;
    };
    console.log(validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]))
  </script>
</body>

</html>